//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "YoYInflationCouponPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Termstructures/Volatility/Inflation/YoYOptionletVolatilitySurface.h>
#include <gen/QL/Cashflows/InflationCoupon.h>
#include <gen/QL/Cashflows/InflationCouponPricer.h>
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>^ capletVol) : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
    CYoYOptionletVolatilitySurface^ _CcapletVol;
    try
    {
#ifdef HANDLE
        _phYoYInflationCouponPricer = NULL;
#endif
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>::IsSome::get (capletVol))
        {
            _CcapletVol = safe_cast<CYoYOptionletVolatilitySurface^> (capletVol->Value);
            _CcapletVol->Lock();
        }
        Handle<QuantLib::YoYOptionletVolatilitySurface>& _capletVol = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>::IsSome::get (capletVol) ? static_cast<Handle<QuantLib::YoYOptionletVolatilitySurface>&> (_CcapletVol->GetHandle ()) : Handle<QuantLib::YoYOptionletVolatilitySurface>()); //1
        _ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (new QuantLib::YoYInflationCouponPricer ( _capletVol ));
        SetInflationCouponPricer (boost::dynamic_pointer_cast<QuantLib::InflationCouponPricer> (*_ppYoYInflationCouponPricer));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcapletVol != nullptr) _CcapletVol->Unlock();
    }
}
Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (boost::shared_ptr<QuantLib::YoYInflationCouponPricer>& childNative, Object^ owner) : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phYoYInflationCouponPricer = NULL;
#endif
	_ppYoYInflationCouponPricer = &childNative;
    _ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::InflationCouponPricer> (*_ppYoYInflationCouponPricer));
}
Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (QuantLib::YoYInflationCouponPricer& childNative, Object^ owner) : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phYoYInflationCouponPricer = NULL;
#endif
	_ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (&childNative);
    _ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::InflationCouponPricer> (*_ppYoYInflationCouponPricer));
    _YoYInflationCouponPricerOwner = owner;
    _InflationCouponPricerOwner = owner;
}

Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (CYoYInflationCouponPricer^ copy) : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phYoYInflationCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (copy->GetShared());
        _ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::InflationCouponPricer> (*_ppYoYInflationCouponPricer));
    }
}
Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (PLATFORM::Type^ t) : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phYoYInflationCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationCouponPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (QuantLib::Handle<QuantLib::YoYInflationCouponPricer>& childNative, Object^ owner)  : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
	_phYoYInflationCouponPricer = &childNative;
	_ppYoYInflationCouponPricer = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCouponPricer>>(childNative.currentLink());
    _ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::InflationCouponPricer> (*_ppYoYInflationCouponPricer));
    _YoYInflationCouponPricerOwner = owner;
}
Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (QuantLib::Handle<QuantLib::YoYInflationCouponPricer> childNative)  : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
	_phYoYInflationCouponPricer = &childNative;
	_ppYoYInflationCouponPricer = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCouponPricer>>(childNative.currentLink());
    _ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::InflationCouponPricer> (*_ppYoYInflationCouponPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CYoYInflationCouponPricer::CYoYInflationCouponPricer (QuantLib::YoYInflationCouponPricer childNative)  : CInflationCouponPricer(CYoYInflationCouponPricer::typeid)
{
#ifdef HANDLE
	_phYoYInflationCouponPricer = NULL;
#endif
	_ppYoYInflationCouponPricer = new boost::shared_ptr<QuantLib::YoYInflationCouponPricer> (new QuantLib::YoYInflationCouponPricer (childNative));
    _ppInflationCouponPricer = new boost::shared_ptr<QuantLib::InflationCouponPricer> (boost::dynamic_pointer_cast<QuantLib::InflationCouponPricer> (*_ppYoYInflationCouponPricer));
}
#endif

Cephei::QL::Cashflows::CYoYInflationCouponPricer::~CYoYInflationCouponPricer ()
{
    if (_ppYoYInflationCouponPricer != NULL)
    {
	    delete _ppYoYInflationCouponPricer;
        _ppYoYInflationCouponPricer = NULL;
    }
}
Cephei::QL::Cashflows::CYoYInflationCouponPricer::!CYoYInflationCouponPricer ()
{
    if (_ppYoYInflationCouponPricer != NULL)
    {
	    delete _ppYoYInflationCouponPricer;
    }
}
QuantLib::YoYInflationCouponPricer& Cephei::QL::Cashflows::CYoYInflationCouponPricer::GetReference ()
{
    if (_ppYoYInflationCouponPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppYoYInflationCouponPricer;
}
boost::shared_ptr<QuantLib::YoYInflationCouponPricer>& Cephei::QL::Cashflows::CYoYInflationCouponPricer::GetShared ()
{
    if (_ppYoYInflationCouponPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppYoYInflationCouponPricer;
}
QuantLib::YoYInflationCouponPricer* Cephei::QL::Cashflows::CYoYInflationCouponPricer::GetPointer ()
{
    if (_ppYoYInflationCouponPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppYoYInflationCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationCouponPricer>& Cephei::QL::Cashflows::CYoYInflationCouponPricer::GetHandle ()
{
	if (_phYoYInflationCouponPricer == NULL)
	{
		_phYoYInflationCouponPricer = new Handle<QuantLib::YoYInflationCouponPricer> (*_ppYoYInflationCouponPricer);
	}
	return *_phYoYInflationCouponPricer;
}
#endif
bool Cephei::QL::Cashflows::CYoYInflationCouponPricer::HasNative () 
{
	return (_ppYoYInflationCouponPricer != NULL);
}

Double Cephei::QL::Cashflows::CYoYInflationCouponPricer::CapletPrice (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppYoYInflationCouponPricer)->capletPrice ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CYoYInflationCouponPricer::CapletRate (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationCouponPricer)->capletRate ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ Cephei::QL::Cashflows::CYoYInflationCouponPricer::CapletVolatility::get ()
{
    try
    {
    	Handle<QuantLib::YoYOptionletVolatilitySurface> _rv = (Handle<QuantLib::YoYOptionletVolatilitySurface>)(*_ppYoYInflationCouponPricer)->capletVolatility ( );   
        Cephei::QL::Termstructures::Volatility::Inflation::CYoYOptionletVolatilitySurface^ _nrv = REFNEW Cephei::QL::Termstructures::Volatility::Inflation::CYoYOptionletVolatilitySurface (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CYoYInflationCouponPricer::FloorletPrice (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppYoYInflationCouponPricer)->floorletPrice ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CYoYInflationCouponPricer::FloorletRate (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationCouponPricer)->floorletRate ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IYoYInflationCouponPricer^ Cephei::QL::Cashflows::CYoYInflationCouponPricer::Initialize (Cephei::QL::Cashflows::IInflationCoupon^ coupon)
{
    CInflationCoupon^ _Ccoupon;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccoupon = safe_cast<CInflationCoupon^> (coupon);
        _Ccoupon->Lock();
        QuantLib::InflationCoupon& _coupon = static_cast<QuantLib::InflationCoupon&> (_Ccoupon->GetReference ()); 
    	(*_ppYoYInflationCouponPricer)->initialize ( _coupon );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccoupon != nullptr) _Ccoupon->Unlock();
    }
}
Cephei::QL::Cashflows::IYoYInflationCouponPricer^ Cephei::QL::Cashflows::CYoYInflationCouponPricer::SetCapletVolatility (Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ capletVol)
{
    CYoYOptionletVolatilitySurface^ _CcapletVol;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcapletVol = safe_cast<CYoYOptionletVolatilitySurface^> (capletVol);
        _CcapletVol->Lock();
        Handle<QuantLib::YoYOptionletVolatilitySurface>& _capletVol = static_cast<Handle<QuantLib::YoYOptionletVolatilitySurface>&> (_CcapletVol->GetHandle ()); 
    	(*_ppYoYInflationCouponPricer)->setCapletVolatility ( _capletVol );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcapletVol != nullptr) _CcapletVol->Unlock();
    }
}
Double Cephei::QL::Cashflows::CYoYInflationCouponPricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppYoYInflationCouponPricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CYoYInflationCouponPricer::SwapletRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationCouponPricer)->swapletRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IYoYInflationCouponPricer^ Cephei::QL::Cashflows::CYoYInflationCouponPricer_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^>^ capletVol)
{
    return REFNEW CYoYInflationCouponPricer ( capletVol);
}
